package com.pm.service.impl;

import com.pm.entity.Issue;
import com.pm.mapper.IssueMapper;
import com.pm.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 问题服务实现类
 */
@Service
public class IssueServiceImpl implements IssueService {
    
    @Autowired
    private IssueMapper issueMapper;
    
    @Override
    public Issue createIssue(Issue issue) {
        issue.setCreatedAt(LocalDateTime.now().toString());
        issue.setUpdatedAt(LocalDateTime.now().toString());
        if (issue.getStatus() == null) {
            issue.setStatus("OPEN");
        }
        if (issue.getPriority() == null) {
            issue.setPriority("MEDIUM");
        }
        if (issue.getType() == null) {
            issue.setType("BUG");
        }
        issueMapper.insert(issue);
        return issue;
    }
    
    @Override
    public Issue updateIssue(Issue issue) {
        issue.setUpdatedAt(LocalDateTime.now().toString());
        issueMapper.updateById(issue);
        return issue;
    }
    
    @Override
    public boolean deleteIssue(String id) {
        return issueMapper.deleteById(id) > 0;
    }
    
    @Override
    public Issue findById(String id) {
        return issueMapper.selectById(id);
    }
    
    @Override
    public List<Issue> getAllIssues() {
        return issueMapper.selectList(null);
    }
    
    @Override
    public List<Issue> findByProjectId(String projectId) {
        return issueMapper.findByProjectId(projectId);
    }
    
    @Override
    public List<Issue> findByReporterId(String reporterId) {
        return issueMapper.findByReporterId(reporterId);
    }
    
    @Override
    public List<Issue> findByAssigneeId(String assigneeId) {
        return issueMapper.findByAssigneeId(assigneeId);
    }
    
    @Override
    public List<Issue> findByStatus(String status) {
        return issueMapper.findByStatus(status);
    }
    
    @Override
    public List<Issue> findByPriority(String priority) {
        return issueMapper.findByPriority(priority);
    }
    
    @Override
    public boolean updateIssueStatus(String id, String status) {
        Issue issue = findById(id);
        if (issue == null) {
            throw new RuntimeException("问题不存在");
        }
        
        issue.setStatus(status);
        issue.setUpdatedAt(LocalDateTime.now().toString());
        
        // 如果状态为已解决，设置解决时间
        if ("RESOLVED".equals(status) || "CLOSED".equals(status)) {
            issue.setResolvedAt(LocalDateTime.now().toString());
        }
        
        return issueMapper.updateById(issue) > 0;
    }
    
    @Override
    public boolean assignIssue(String id, String assigneeId) {
        Issue issue = findById(id);
        if (issue == null) {
            throw new RuntimeException("问题不存在");
        }
        
        issue.setAssigneeId(assigneeId);
        issue.setUpdatedAt(LocalDateTime.now().toString());
        
        return issueMapper.updateById(issue) > 0;
    }
    
    @Override
    public boolean resolveIssue(String id) {
        Issue issue = findById(id);
        if (issue == null) {
            throw new RuntimeException("问题不存在");
        }
        
        issue.setStatus("RESOLVED");
        issue.setResolvedAt(LocalDateTime.now().toString());
        issue.setUpdatedAt(LocalDateTime.now().toString());
        
        return issueMapper.updateById(issue) > 0;
    }
}